package com.hengxinyongli.iam.web.controller;

import com.github.pagehelper.PageInfo;
import com.hengxinyongli.iam.entity.Role;
import com.hengxinyongli.iam.mapper.RoleResourceMapper;
import com.hengxinyongli.iam.service.RoleService;
import com.hengxinyongli.iam.utils.AssertUtils;
import com.hengxinyongli.iam.web.constant.Url;
import com.hengxinyongli.iam.web.vo.Responses;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.Objects;
import java.util.Set;

/**
 * @author magic
 * @date 2018/9/25/025 14:23
 * @version 1.0
 * Description RoleController
 */
@RestController
@Api(description = "用户角色相关api")
public class RoleController {

    @Autowired
    RoleService roleService;

    @GetMapping(Url.ROLES)
    @ApiOperation(value = "获取所有角色列表",notes = "获取所有角色列表")
    public Responses roles(@RequestParam(required = false) @ApiParam(value = "分页查询页号码") Integer pageNo,
                           @RequestParam(required = false) @ApiParam(value = "分页查询页大小") Integer pageSize,
                           @RequestParam(required = false) @ApiParam(value = "模糊查询角色名字") String roleName){
        PageInfo pageInfo = roleService.selectRoles(pageNo,pageSize,roleName);
        return Responses.success(pageInfo);
    }

    @GetMapping(Url.ROLES_ONE)
    @ApiOperation(value = "获取指定的角色信息",notes = "获取指定的角色信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Role.class)
    )
    public Responses rolesOne(@PathVariable @ApiParam(value = "角色编号") String id){
        Role role = roleService.selectByIdWithRes(id);
        return Responses.success(role);
    }

    @PostMapping(Url.ROLES)
    @ApiOperation(value = "创建角色信息",notes = "创建角色信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功",response = Role.class)
    )
    public Responses rolesCreate(@RequestBody @Validated RoleRo roleRo){
        Role role = new Role();
        role.setDescription(roleRo.getDescription());
        role.setName(roleRo.getName());
        role.setParentId(roleRo.getParentId());
        role.setCreateAt(new Date());
        roleService.roleCreate(role,roleRo.getPermits());
        return Responses.success(roleRo);
    }

    @PutMapping(Url.ROLES_ONE)
    @ApiOperation(value = "修改指定的角色信息",notes = "修改指定的角色信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功")
    )
    public Responses rolesUpdate(@PathVariable @ApiParam(value = "角色编号")  String id,@RequestBody @Validated RoleRo roleRo){
        roleService.roleUpdate(id,roleRo);
        return Responses.success(id);
    }

    @DeleteMapping(Url.ROLES_ONE)
    @ApiOperation(value = "删除指定的角色信息",notes = "获取指定的角色信息")
    @ApiResponses(
            @ApiResponse(code = 200,message = "操作成功")
    )
    public Responses rolesDelete(@PathVariable @ApiParam(value = "角色编号")  String id){
        roleService.roleDelete(id);
        return Responses.success(id);
    }


    @ApiModel
    public static class RoleRo {

        @ApiModelProperty(value = "角色名称")
        @NotEmpty(message = "角色名称不能为空")
        private String name;
        @ApiModelProperty(value = "角色描述")
        @NotEmpty(message = "角色描述不能为空")
        private String description;
        @ApiModelProperty(value = "角色父编号")
        @NotEmpty(message = "角色父编号不能为空")
        private String parentId;
        @ApiModelProperty(value = "角色权限集合")
        //@NotEmpty(message = "角色权限集合不能为空")
        private Set<String> permits;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public Set<String> getPermits() {
            return permits;
        }

        public void setPermits(Set<String> permits) {
            this.permits = permits;
        }

        public String getParentId() {
            return parentId;
        }

        public void setParentId(String parentId) {
            this.parentId = parentId;
        }

    }
}
